package websocket;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.util.Log;

public class http_interface 
{
	private DefaultHttpClient mHttpClient;
	private MakeJson mkJson;
	private AnalyzeJson anajson;
	public http_interface()
	{
		HttpParams params = new BasicHttpParams();
	    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
	    mHttpClient = new DefaultHttpClient(params);
	    mkJson = new MakeJson();
	    anajson = new AnalyzeJson();
	}
	
	public LogInStruct login(String name,String pass)
	{
		keyBean md5 = new keyBean();
        String passwordmd5 = md5.getkeyBeanofStr(pass);
        return Login(name,passwordmd5);
		
	}
	private LogInStruct Login(String MailAddress, String MD5PassWord)
	{
        LogInStruct login = null;
		try {
	        //HttpPost httppost = new HttpPost("http://test.komugi-office.com:8080/knowledge/web/upload");
	    	HttpPost httppost = new HttpPost("http://test.komugi-office.com:8080/knowledge/web/login_normal");
	    	
	        HttpResponse httpResponse = null;
	        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
	        multipartEntity.addPart("params", new StringBody(mkJson.MakeLoginJson(MailAddress, MD5PassWord).toString()));

	        httppost.setEntity(multipartEntity);

	       // mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
	        httpResponse =  mHttpClient.execute(httppost);
	        
	        HttpEntity r_entity = httpResponse.getEntity();
	        String responseString = EntityUtils.toString(r_entity);
	        String str = responseString;

	        if(responseString != null && responseString != "")
	        	login = anajson.AnalyzeLogin(new JSONObject(responseString));
	        return login;
	      //deal login message

	    } catch (Exception e) {
	        return login;
	    }
	}
}
